package com.qc.fight.example.redis;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import junit.framework.TestCase;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

public class RedisTest extends TestCase {

	public static void main(String[] args) {

	}

	/**
	 * redis 相关测试
	 */
	public static void testInsertSingle() {

		Jedis jedis = new Jedis("192.168.50.128", 7000);

		Map<String, String> map = new HashMap<>();
		long start = System.currentTimeMillis();
		for (int i = 0; i < 200000; i++) {
			jedis.set("sdf" + i, "sssssdfwervx" + i);
		}
		long end = System.currentTimeMillis();
		System.out.println((end - start) / 1000 + "==" + start + "==" + end);
		System.out.println(end - start);
		System.out.println(map.get("sdf" + 100000));

	}

	public void testInsertCluster() {
		JedisPoolConfig poolConfig = new JedisPoolConfig();
		// 最大连接数
		poolConfig.setMaxTotal(3);
		// 最大空闲数
		poolConfig.setMaxIdle(1);
		// 最大允许等待时间，如果超过这个时间还未获取到连接，则会报JedisException异常：
		// Could not get a resource from the pool
		poolConfig.setMaxWaitMillis(1000);
		Set<HostAndPort> nodes = new LinkedHashSet<HostAndPort>();
		nodes.add(new HostAndPort("10.10.11.16", 7000));
		nodes.add(new HostAndPort("10.10.11.16", 7001));
		nodes.add(new HostAndPort("10.10.11.16", 7002));
		nodes.add(new HostAndPort("10.10.11.16", 7003));
		nodes.add(new HostAndPort("10.10.11.16", 7004));
		nodes.add(new HostAndPort("10.10.11.16", 7005));
		JedisCluster cluster = new JedisCluster(nodes, poolConfig);
		long start = System.currentTimeMillis();
		for (int i = 0; i < 200000; i++) {
			cluster.set("sdf" + i, "sssssdfwervx" + i);
		}
		long end = System.currentTimeMillis();
		System.out.println((end - start) / 1000 + "==" + start + "==" + end);
		System.out.println(end - start);
		try {
			cluster.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
